/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.orm.refinedclasses;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import org.testis.orm.classes.T_bed_devicedefinitions;
import org.testis.orm.classes.T_bed_firmware;
import org.testis.utils.middleware.DeviceDefinitionDataHandler;

/**
 * 
 * @author selcuk
 */
public class DeviceDefinitionRefined extends TestBedItem {

    private T_bed_devicedefinitions m_reference;

    public DeviceDefinitionRefined(T_bed_devicedefinitions reference) {
	super();
	setReference(reference);
    }

    public int getId() {
	return m_reference.getId();
    }

    public String getName() {
	return m_reference.getName();
    }

    public LinkedList<DeviceFirmwareRefined> getFirmwareList() {
	LinkedList<DeviceFirmwareRefined> result = new LinkedList<DeviceFirmwareRefined>();
	T_bed_firmware[] firmwares = m_reference.t_bed_firmware.toArray();
	if (firmwares != null) {
	    for (int i = 0; i < firmwares.length; i++) {
		result.add(new DeviceFirmwareRefined(firmwares[i]));
	    }
	}
	return result;
    }

    private void setReference(T_bed_devicedefinitions reference) {
	if (reference == null) {
	    throw new NullPointerException();
	}
	m_reference = reference;
    }

    @Override
    public String toString() {
	return getName();
    }

    @Override
    public String getTypeName() {
	return "DeviceDefinition";
    }

    public static DeviceDefinitionRefined[] getDeviceDefinitionArray() {
	Collection coll = DeviceDefinitionDataHandler.newInstance()
		.getDeviceDefinitions();
	DeviceDefinitionRefined[] result = new DeviceDefinitionRefined[coll
		.size()];
	int idx = 0;
	for (Iterator it = coll.iterator(); it.hasNext(); idx++) {
	    T_bed_devicedefinitions ref = (T_bed_devicedefinitions) it.next();
	    result[idx] = new DeviceDefinitionRefined(ref);
	}
	return result;
    }

    public static LinkedList<DeviceDefinitionRefined> getDeviceDefinitionList() {
	LinkedList<DeviceDefinitionRefined> result = new LinkedList<DeviceDefinitionRefined>();
	DeviceDefinitionRefined[] arr = getDeviceDefinitionArray();
	for (int i = 0; i < arr.length; i++) {
	    result.add(arr[i]);
	}
	return result;
    }

    public static HashMap getDeviceDefinitionMap() {
	HashMap result = new HashMap();
	DeviceDefinitionRefined[] arr = getDeviceDefinitionArray();
	for (int i = 0; i < arr.length; i++) {
	    result.put(arr[i].getId(), arr[i]);
	}
	return result;
    }

    public static void main(String args[]) {
	// test getDeviceDefinitionMap function
	HashMap deviceDefMap = DeviceDefinitionRefined.getDeviceDefinitionMap();
	for (Iterator it = deviceDefMap.keySet().iterator(); it.hasNext();) {
	    DeviceDefinitionRefined device = (DeviceDefinitionRefined) deviceDefMap
		    .get(it.next());
	    System.out.println(device.getId() + ": " + device.getName());
	    LinkedList<DeviceFirmwareRefined> fwList = device.getFirmwareList();
	    for (Iterator<DeviceFirmwareRefined> it2 = fwList.iterator(); it2
		    .hasNext();) {
		DeviceFirmwareRefined deviceFirmwareRefined = it2.next();
		System.out.println("   " + deviceFirmwareRefined.getName()
			+ "'" + deviceFirmwareRefined.getDescription() + "'");
	    }
	}
    }
}
